package com.graduation.common.autoconfigure.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.graduation.common.autoconfigure.mq.handler.MqMessageHelper;
import com.graduation.common.autoconfigure.mq.handler.RabbitMqMessageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "legal.mq", value = "enabled")
public class RabbitMqConfig {

    @Bean
    public MessageConverter messageConverter(ObjectMapper mapper) {
        // 1.定义消息转换器
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(mapper);
        // 2.配置自动创建消息id，用于识别不同消息
        jackson2JsonMessageConverter.setCreateMessageIds(true);
        return jackson2JsonMessageConverter;
    }

    @Bean
    public MqMessageHelper mqMessageHelper(RabbitTemplate rabbitTemplate) {
        log.info("rabbitmq初始化中。。。");
        MqMessageHelper mqMessageHelper = new RabbitMqMessageHelper(rabbitTemplate);
        log.info("rabbitmq初始化完成。。。");
        return mqMessageHelper;
    }

}
